<?php
$adm_menu = "1";

include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") header('Location: index.php');

$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);

$err = "";

if (isset($_REQUEST["uid"]) && isset($_REQUEST["act"]) && ($_REQUEST["act"] == "del")) {
	$uid = addslashes_mssql($_REQUEST["uid"]);
	$sql = "select count(*) as cnt
	from aparatai					
	where Aparatai.aparatoid= '".$uid."'";	
	$info = $db_rdata->returnInfo($sql);	
	if ($info["cnt"] != 0) {
		$sql = "select count(*) as cnt
		from kvitogalva					
		where aparatoid = '".$uid."'";	
		$info = $db_rdata->returnInfo($sql);	
		if ($info["cnt"] == 0) {
				$terminal_del_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',$uid), $terminal_del_query);
				$db_rdata->execute($terminal_del_query);
				
				$terminal_delr_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',$uid), $terminal_delr_query);
				$db_www->execute($terminal_delr_query);
				header("Location: terminals.php");			
		} else {
			$err .= "<br>Aparato trinti negalima, turi galiojančių kvitų!";
		}
	} else {
		$err .= "<br>Nerastas aparatas su id: ".$uid."!";
	}
	
}

if (isset($_REQUEST["findim"])){
		$pid = @iconv('UTF-8','windows-1257', trim($_POST["pid"]));
		$pad = $db_www->returnInfo("select n07_pav from n07_imon where n07_kodas_is='".addslashes_mssql($pid)."'");
		if (!empty($pad)) {
			$ppav = iconv('windows-1257', 'UTF-8', $pad["n07_pav"]);
		} else {
			
			$ppav = "";
			$err .= "<br>Nerastas padalinys su tokiu kodu!";
		}

}
if (isset($_REQUEST["naujas"])) {
	$naujas = $_REQUEST["naujas"];
} else {
	if (!isset($_REQUEST["uid"])) {
		$naujas = "1";
	} else if (isset($_REQUEST["uid"]) && ($_REQUEST["uid"]=="0")) {
		$naujas = "1";
	} else {
		$naujas = "0";
	}
}
if (isset($_REQUEST["act"]) && !isset($_REQUEST["findim"])){
	 if ($_REQUEST["act"] == "save") {
		$pid = $_POST["pid"];
		$pad = $db_www->returnInfo("select n07_pav from n07_imon where n07_kodas_is='".addslashes_mssql($pid)."'");
		if (!empty($pad)) {				
			$ppav = @iconv('windows-1257', 'UTF-8', $pad["n07_pav"]);
		} else {				
			$ppav = "";
			$err .= "<br>Nerastas padalinys su tokiu kodu!";
		}
		$pav = $_POST["pav"];
		if (trim($pav) == "") {
			$err .= "<br>Neįvestas aparato pavadinimas!";
		}
	 
		if ($err == "") {
			if ($naujas != "1") {	 
				if (isset($_POST["apzvalga"]) && strtolower($_POST["apzvalga"]=="on")) {
					$apzvalga = '1';
				} else {
					$apzvalga = '0';
				}
				$terminal_change_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $terminal_change_query);
				$terminal_change_query = str_replace("<role_id>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["role_id"])), $terminal_change_query);
				$terminal_change_query = str_replace("<pass>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pass"])), $terminal_change_query);
				$terminal_change_query = str_replace("<apzvalga>", @iconv('UTF-8','windows-1257',addslashes_mssql($apzvalga)), $terminal_change_query);
				$terminal_change_query = str_replace("<pav>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pav"])), $terminal_change_query);

				$db_rdata->execute($terminal_change_query);
				
				$terminal_changer_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $terminal_changer_query);
				$terminal_changer_query = str_replace("<pid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pid"])), $terminal_changer_query);
				$terminal_changer_query = str_replace("<lastz>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["lastz"])), $terminal_changer_query);

				$db_www->execute($terminal_changer_query);
				//echo $terminal_changer_query;die();
				header("Location: terminals.php");
			} else {
				$uid = $_REQUEST["uid"];
				$sql = "select count(*) as cnt
				from aparatai					
				where Aparatai.aparatoid= '".addslashes_mssql($uid)."'";
				
				
				$info = $db_rdata->returnInfo($sql);	
				if ($info["cnt"] == 0) {
					if (is_numeric($uid)) {
						$aparatas_insert_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $aparatas_insert_query);
						$db_rdata->execute($aparatas_insert_query);

						if (isset($_POST["apzvalga"]) && strtolower($_POST["apzvalga"]=="on")) {
							$apzvalga = '1';
						} else {
							$apzvalga = '0';
						}
						$terminal_change_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $terminal_change_query);
						$terminal_change_query = str_replace("<role_id>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["role_id"])), $terminal_change_query);
						$terminal_change_query = str_replace("<pass>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pass"])), $terminal_change_query);
						$terminal_change_query = str_replace("<apzvalga>", @iconv('UTF-8','windows-1257',addslashes_mssql($apzvalga)), $terminal_change_query);
						$terminal_change_query = str_replace("<pav>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pav"])), $terminal_change_query);

						$db_rdata->execute($terminal_change_query);		
						
						$aparatas_insertr_query = str_replace("<uid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $aparatas_insertr_query);
						$aparatas_insertr_query = str_replace("<pid>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pid"])), $aparatas_insertr_query);
						$aparatas_insertr_query = str_replace("<lastz>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["lastz"])), $aparatas_insertr_query);
						$db_www->execute($aparatas_insertr_query);
						
					
						
						header("Location: terminals.php");
					} else {
						$err .= "<br>Aparato ID turi būti sveikas skaičius!";
					}	
					
				} else {
					$err .= "<br>Aparatas su tokiu ID jau egzistuoja!";
				}
			}
		}
	}
}	
include("header.php");
include("menu.php");




	if (isset($_REQUEST["uid"]) && $naujas == "0") {
		$uid = @iconv('UTF-8','windows-1257',$_REQUEST["uid"]);
		
		if (!isset($_POST["uid"])){
			$sql = "select AparatoPavadinimas,Aparatai.apzvalga,ws_users.role_id,ws_users.user_id
			from aparatai
			
			
			left outer join  ws_users on Aparatai.AparatoID=ws_users.AparatoID 
			where Aparatai.aparatoid= '".addslashes_mssql($uid)."' 
			
			group by Aparatai.AparatoPavadinimas,Aparatai.apzvalga,ws_users.role_id,ws_users.user_id";
			
			$info = $db_rdata->returnInfo($sql);	

			
		
			
			if (!empty($info)) {
				$pav = @iconv('windows-1257', 'UTF-8', $info["AparatoPavadinimas"]);
				if ($info["apzvalga"]==1) {
					$apzvalga = true;
				} else {
					$apzvalga = false;
				}
				$role_id = $info["role_id"];
				$pass = $info["user_id"];
			} else {
				$pav = "";
				$apzvalga = false;
				$role_id = "";
				$pass = "";
			}
			$pad = $db_www->returnInfo("select I24_KODAS_IS,n07_pav,I24_PAR_TXT15 from I24_RYS,n07_imon where n07_kodas_is=I24_KODAS_IS and I24_KODAS_KK='".addslashes_mssql($uid)."'");
			if (!empty($pad)) {
				$pid = @iconv('windows-1257', 'UTF-8', trim($pad["I24_KODAS_IS"]));
				$ppav = @iconv('windows-1257', 'UTF-8', $pad["n07_pav"]);
				$zeto = @iconv('windows-1257', 'UTF-8', trim($pad["I24_PAR_TXT15"]));
			} else {
				$pid = "";
				$ppav = "";
				$zeto = "";
			}
		} else {
				$pav = $_POST["pav"];
				$zeto = $_POST["lastz"];
				$pid = $_POST["pid"];
				$pad = $db_www->returnInfo("select n07_pav from n07_imon where n07_kodas_is='".addslashes_mssql($pid)."'");
				
				if (!empty($pad)) {
					
					$ppav = @iconv('windows-1257', 'UTF-8', $pad["n07_pav"]);
				} else {
					
					$ppav = "";

				}
				if (isset($_POST["apzvalga"]) && strtolower($_POST["apzvalga"]=="on")) {
					$apzvalga = true;
				} else {
					$apzvalga = false;
				}
				$role_id = $_POST["role_id"];
				$pass = $_POST["pass"];
		}
	} else {
		

		
		if (!isset($_POST["uid"])){
			$pav = "";
			$zeto = "";
			$pid = "";
			$ppav = "";
			$apzvalga = false;
			$role_id = "";
			$pass = "";
			$uid = "0";
		} else {
			$uid = $_POST["uid"];
			$pav = $_POST["pav"];
			$zeto = $_POST["lastz"];
			$pid = $_POST["pid"];
			$pad = $db_www->returnInfo("select n07_pav from n07_imon where n07_kodas_is='".trim(addslashes_mssql($pid))."'");
			
			if (!empty($pad)) {			
		
				$ppav = @iconv('windows-1257', 'UTF-8', $pad["n07_pav"]);
			} else {
				
				$ppav = "";

			}	
			if (isset($_POST["apzvalga"]) && strtolower($_POST["apzvalga"]=="on")) {
				$apzvalga = true;
			} else {
				$apzvalga = false;
			}	
			$role_id = $_POST["role_id"];
			$pass = $_POST["pass"];			
		}
	}
	

	



?>
<script>
function validateForm(myform)
{
test ();
  pav = document.getElementById("pav");
	if (pav.value.trim() == "") {
		alert("Pavadinimas yra privalomas");
		return;
	}
	myform.submit();
}
</script>
<SCRIPT language="JavaScript">
<!--

function isUnsignedInteger(s) {
  return (s.toString().search(/^[0-9]+$/) == 0);
}



function getRandomNum(lbound, ubound) {
return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandomChar(number, lower, upper, other, extra) {
var numberChars = "0123456789";
var lowerChars = "abcdefghijklmnopqrstuvwxyz";
var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var otherChars = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";
var charSet = extra;
if (number == true)
charSet += numberChars;
if (lower == true)
charSet += lowerChars;
if (upper == true)
charSet += upperChars;
if (other == true)
charSet += otherChars;
return charSet.charAt(getRandomNum(0, charSet.length));
}

function getPassword(length, extraChars, firstNumber, firstLower, firstUpper, firstOther,
latterNumber, latterLower, latterUpper, latterOther) {
var rc = "";
if (length > 0)
rc = rc + getRandomChar(firstNumber, firstLower, firstUpper, firstOther, extraChars);
for (var idx = 1; idx < length; ++idx) {
rc = rc + getRandomChar(latterNumber, latterLower, latterUpper, latterOther, extraChars);
}
return rc;
}

function generate(){	
	document.forma.pass.value = getPassword(8,true,true,true,true,false,true,true,true,false);
}


//-->
</SCRIPT>


<form method="post" name='forma' action='terminal.php?act=save'>
<?php
if ($naujas != "1") {
?>
<input type="hidden" name="uid" value="<?php echo $uid;?>">
<?php
}
?>
<input type="hidden" name="naujas" value="<?php echo $naujas;?>">

<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">
<input class='but bgwhite nopad' type = "submit" value = "Išsaugoti" onclick="validateForm('forma');return false;"><br><br>
<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='terminals.php';return false;">Atgal</Button>
</td><td class="centercol">
<?php
if ($err != ""){
	echo "<p style='background-color:transparent;color:red;font-weight:bold;font-size:10px;'>".$err."</p>";
}
?>


<table width="100%" border="0">
<tr><td valign="top"  class="fieldtitle">Aparato rolė:</td><td valign="top" class="fieldtitle">Rodyti apžvalgoje:</td></tr>
<tr><td valign="top"  >
<script  type="text/javascript">
	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
</script> 	
<div style="position: relative;">
<select name="role_id" class="select" id='role_id'>
<option value=" " ></option>
<?php
$sql = "select * from ws_roles_h order by role_pav";
$list = $db_rdata->returnList($sql);
	foreach ($list as $item) {
		$lid = @iconv('windows-1257', 'UTF-8', trim($item["role_id"]));
		$lpav = @iconv('windows-1257', 'UTF-8', trim($item["role_pav"]));
		if ($role_id == trim($item["role_id"])){
		   $sel ="selected";
		} else {
		   $sel = "";
		}
		echo '<option value="'.$lid.'" '.$sel.'>'.htmlspecialchars($lpav).'</option>';
	}
?>
</select>
</div>
</td><td valign="top" ><input  id="apzvalga"   type='checkbox' name="apzvalga" <?php if ($apzvalga) echo "CHECKED";?>></td></tr>



<tr><td valign="top" class="fieldtitle">Aparato Id:</td><td valign="top" class="fieldtitle">Pavadinimas:</td></tr>
<tr>
<td valign="top">
<?php
if ($naujas != "1") {
?>
<input  id='uid' class="inp" type='text' name="uid" value="<?php echo htmlspecialchars($uid)?>" DISABLED>
<?php 
} else {
if ($uid != "0") {
	$uuu = $uid;
} else {
	$uuu = "";
}
?>
<input  id='uid' class="inp" type='text' name="uid" value="<?php echo htmlspecialchars($uuu)?>">
<?php } ?>
</td>
<td valign="top" >
<input style="width:230px;" id='pav' class="inp" type='text' name="pav" value="<?php echo htmlspecialchars($pav)?>">
</td>
</tr>
<tr><td valign="top" class="fieldtitle">Padalinio Id:</td><td valign="top" class="fieldtitle">Padalinio pavadinimas:</td></tr>
<tr>
<td valign="top">
<input  id='pid' class="inp" type='text' name="pid" value="<?php echo htmlspecialchars($pid)?>" >
<br>
<div style="float:right;padding-right:10px;"><A class='flink' href="#" onclick='document.forma.action="terminal.php?act=save&findim=1";document.forma.submit()'>Ieškoti</A></div>

</td>
<td valign="top" >
<input style="width:230px;" id='ppav' class="inp" type='text' name="ppav" value="<?php echo htmlspecialchars($ppav)?>" DISABLED>
</td>
</tr>
<tr><td valign="top" class="fieldtitle">Paskutinis Z:</td><td valign="top" class="fieldtitle">WEB serviso slaptažodis:</td></tr>
<tr>
<td valign="top">
<input  id='lastz' class="inp" type='text' name="lastz" value="<?php echo htmlspecialchars($zeto)?>" >
</td>
<td valign="top" >
<table cellspacing="0" cellpadding="0"><tr><td>
<input style="width:230px;" id='pass' class="inp" type='text' name="pass" value="<?php echo htmlspecialchars($pass)?>" >
</td></tr><tr><td align="right">
<a class="flink" href="javascript:generate()">Generuoti</a>
</td></tr></table>
</td>
</tr>
<?php
if ($naujas != "1") {
?>
<tr><td colspan="2"><br><button id="unblockButton" class="but bgwhite nopad" onclick="window.location='terminal.php?act=del&uid=<?php echo $uid;?>';return false;">Trinti</button></td></tr>
<?php
}
?>
</table>


</td><td class="rightcol">
<?php

?>


</td></tr>
<tr><td>&nbsp;</td><td colspan="2">

</td></tr>
</table>
</form>
<?php
include("footer.php");
?>